package kr.saeildamil.androidstudy_second31;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;

public class GestureNavi extends Activity {
	TextView mResult;
	TextView mTextCount;
	
	int mCount=10;
	GestureDetector mDetector;
	final static int DISTANCE=200;
	final static int VELOCITY=300;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch31_gesturenavi);
		
		mResult=(TextView) findViewById(R.id.result);
		mTextCount=(TextView) findViewById(R.id.count);
		
		mDetector=new GestureDetector(this, mGestureListener);
		mDetector.setIsLongpressEnabled(false);
		mTextCount.setText(Integer.toString(mCount));
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		return mDetector.onTouchEvent(event);
	};
	OnGestureListener mGestureListener=new OnGestureListener() {
		
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if(Math.abs(velocityX)>VELOCITY){//좌우로 fling했을때 기준 가속도
				if(e1.getX()-e2.getX()>DISTANCE || e1.getY()-e2.getY()>DISTANCE){
					//우에서 좌로 이동
					mCount--;
				}
				if(e2.getX()-e1.getX()>DISTANCE){
					//좌에서 우로 이동
					mCount++;
					}
				mTextCount.setText(Integer.toString(mCount));
				String text=String.format("거리 = %d,  속도 = %d",(int)Math.abs(e1.getX()-e2.getX()),(int)velocityX);
				mResult.setText(text);
				}
			
			return true;
		}
		
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public void onShowPress(MotionEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
				float distanceY) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public void onLongPress(MotionEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		
		
		@Override
		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
			return false;
		}
	};
}
